package com.xhsj.user.pms.oareply.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.generalprocess.mapper.TbPamGeneralNodeMapper;
import com.xhsj.user.pms.oareply.dto.*;
import com.xhsj.user.pms.oareply.service.*;
import com.xhsj.user.pms.oareply.vo.CorrectionFileListVo;
import com.xhsj.user.pms.oareply.vo.FindToAuditVo;
import com.xhsj.user.pms.patent.vo.ProcessNodeVo;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.web.bind.annotation.RestController;


import com.xhsj.user.pms.oareply.entity.TbPamPatentOaCorrection;

import javax.validation.Valid;


/**
 * <p>
 * 专利OA补正 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Jul 28, 2021 8:03:42 AM
 */
@Slf4j
@Api(tags = "专利OA补正")
@RestController
@RequestMapping("/oareply/tbPamPatentOaCorrection")
public class TbPamPatentOaCorrectionController {

	@Autowired
    public TbPamPatentOaCorrectionService service;

	@Autowired
	public TbPamPatentOaRecheckFileService tbPamPatentOaRecheckFileService;

	@Autowired
	public TbPamPatentOaCorrectionFileService tbPamPatentOaCorrectionFileService;

	@Autowired
	public TbPamPatentOaFileService tbPamPatentOaFileService;

	@Autowired
	private TbPamGeneralNodeMapper tbPamGeneralNodeMapper;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Jul 28, 2021 8:03:42 AM
	 */
	@ApiOperation(value = "专利OA补正,保存/修改", notes = "专利OA补正,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:pam:patent:oa:correction:edit')")
	public Message save(@RequestBody TbPamPatentOaCorrection data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}
	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Jul 28, 2021 8:03:42 AM
	 */
	@ApiOperation(value = "专利OA补正删除", notes = "通过id删除专利OA补正信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:pam:patent:oa:correction:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}
	}


    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Jul 28, 2021 8:03:42 AM
    */
	@ApiOperation(value = "分页查询专利OA补正list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:pam:patent:oa:correction:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbPamPatentOaCorrection> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Jul 28, 2021 8:03:42 AM
    */
	@ApiOperation(value = "查询专利OA补正所有数据list", notes = "查询专利OA补正所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:pam:patent:oa:correction:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Jul 28, 2021 8:03:42 AM
    */
	@ApiOperation(value = "通过id获取专利OA补正实体类", notes = "通过id获取专利OA补正实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

	   /**
	       * @Description: 专利OA补正流程启动
	       * @Param:
	       * @return:
	       * @Author: liuqinghua
	       * @Date: 2021/7/28
	       */
	   @ApiOperation(value = "专利OA补正流程启动 ", notes = "专利OA补正流程启动")
	   @RequestMapping(value="/startOACorrectionProcess",method = RequestMethod.POST)
	   public Message startOACorrectionProcess(@RequestBody @Valid StartOACorrectionProcessDto param, BindingResult bindingResult) {
		   if (bindingResult.hasErrors()) {
			   log.info(bindingResult.getFieldError().getDefaultMessage());
			   return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		   }
		   try {
			   service.startOACorrectionProcess(param);
		   } catch (Exception e) {
			   e.printStackTrace();
			   return Message.error(1,e.getMessage());
		   }
		   return Message.success("成功");
	   }

	      /**
	          * @Description:  专利OA补正-IPR/操作人补正文件流程
	          * @Param:
	          * @return:
	          * @Author: liuqinghua
	          * @Date: 2021/7/29
	          */
	@ApiOperation(value = "专利OA补正-IPR/操作人补正文件流程", notes = "专利OA补正-IPR/操作人补正文件流程")
	@RequestMapping(value="/iprCorrectionFileProcess",method = RequestMethod.POST)
	public Message iprCorrectionFileProcess(@RequestBody @Valid IprCorrectionFileProcessDto param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		try {
			service.iprCorrectionFileProcess(param);
		} catch (Exception e) {
			e.printStackTrace();
			return Message.error(1,e.getMessage());
		}
		return Message.success("成功");
	}

	   /**
	       * @Description:  专利OA补正-知产部负责人流程
	       * @Param:
	       * @return:
	       * @Author: liuqinghua
	       * @Date: 2021/7/29
	       */
	@ApiOperation(value = "专利OA补正-知产部负责人流程", notes = "专利OA补正-知产部负责人流程")
	@RequestMapping(value="/zszl1Process",method = RequestMethod.POST)
	public Message zszl1Process(@RequestBody @Valid Zszl1ProcessDto param, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		try {
			service.zszl1Process(param);
		} catch (Exception e) {
			e.printStackTrace();
			return Message.error(1,e.getMessage());
		}
		return Message.success("成功");
	}

	   /**
	       * @Description: 我的申请
	       * @Author: liuqinghua
	       * @Date: 2021/7/29
	       */
	   @ApiOperation(value = "专利OA答复模块-我的申请", notes = "专利OA答复模块-我的申请")
	   @RequestMapping(value="/myApplyList",method = RequestMethod.POST)
	   public Message myApplyList(@RequestBody @Valid MyApplyListDto param,BindingResult bindingResult){
		   if (bindingResult.hasErrors()) {
			   log.info(bindingResult.getFieldError().getDefaultMessage());
			   return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		   }
		   try {
               IPage<FindToAuditVo> findToAuditVoIPage = service.myApplyList(param);
               return Message.success("成功",findToAuditVoIPage.getRecords(),findToAuditVoIPage.getTotal(),findToAuditVoIPage.getPages());
           } catch (Exception e) {
			   e.printStackTrace();
			   return Message.error(1,e.getMessage());
		   }

	   }

	      /**
	          * @Description: 待审核
	          * @Param:
	          * @return:
	          * @Author: liuqinghua
	          * @Date: 2021/7/29
	          */
		  @ApiOperation(value = "专利OA补正-待审核", notes = "专利OA补正-待审核")
		  @RequestMapping(value="/findToAudit",method = RequestMethod.POST)
		  public Message findToAudit(@RequestBody @Valid FindToAuditDto param,BindingResult bindingResult) {
			  try {
                  if (bindingResult.hasErrors()) {
                      log.info(bindingResult.getFieldError().getDefaultMessage());
                      return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
                  }
				  IPage<FindToAuditVo> toAudit = service.findToAudit(param);
				  return Message.success("成功",toAudit.getRecords(),toAudit.getTotal(),toAudit.getPages());
			  } catch (Exception e) {
				  e.printStackTrace();
				  return Message.error(1,"失败");
			  }

		  }
   /**
       * @Description:  获取补正文件-分页
       * @Author: liuqinghua
       * @Date: 2021/8/2
       */
	@ApiOperation(value = "专利OA补正-获取补正文件-分页", notes = "专利OA补正-获取补正文件-分页")
	@RequestMapping(value="/findCorrectionFileList",method = RequestMethod.GET)
	public Message findCorrectionFileList(@RequestParam Long dataId,
										  @RequestParam String processType,
										   @RequestParam(value = "page") Long page,
										   @RequestParam(value = "pageSize") Long pageSize) {
		try {
			IPage<CorrectionFileListVo> fileList = new Page<>();
			switch (processType) {
				case "patent_bz":
					fileList = tbPamPatentOaCorrectionFileService.findCorrectionFileList(dataId,page,pageSize);
					break;
                case "patent_oa":
					fileList = tbPamPatentOaFileService.findOAFileList(dataId,page,pageSize);
                    break;
				case "patent_fs":
					fileList = tbPamPatentOaRecheckFileService.findReCheckFileList(dataId,page,pageSize);
					break;
			}
			return Message.success("成功",fileList.getRecords(),fileList.getTotal(),fileList.getPages());
		} catch (Exception e) {
			e.printStackTrace();
			return Message.error(1,"失败");
		}

	}


    /**
     * @Description:  获取补正文件-不分页
     * @Author: liuqinghua
     * @Date: 2021/8/2
     */
    @ApiOperation(value = "专利OA补正-获取补正文件-不分页", notes = "专利OA补正-获取补正文件-不分页")
    @RequestMapping(value="/findCorrectionFileListNotPage",method = RequestMethod.GET)
    public Message findCorrectionFileListNotPage(@RequestParam Long dataId) {
        try {
            List<CorrectionFileListVo> findCorrectionFileList = service.findCorrectionFileListNotPage(dataId);
            return Message.success("成功",findCorrectionFileList);
        } catch (Exception e) {
            e.printStackTrace();
            return Message.error(1,"失败");
        }

    }

	/**
	 * @Description: 审核通过
	 * @Author: liuqinghua
	 * @Date: 2021/7/29
	 */
	@ApiOperation(value = "专利OA补正-审核通过", notes = "专利OA补正-审核通过")
	@RequestMapping(value="/findApproved",method = RequestMethod.POST)
	public Message findApproved(@RequestBody @Valid FindToAuditDto param,BindingResult bindingResult) {
		try {
            if (bindingResult.hasErrors()) {
                log.info(bindingResult.getFieldError().getDefaultMessage());
                return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
            }
			IPage<FindToAuditVo> toAudit = service.findApproved(param);
			return Message.success("成功",toAudit.getRecords(),toAudit.getTotal(),toAudit.getPages());
		} catch (Exception e) {
			e.printStackTrace();
			return Message.error(1,"失败");
		}

	}

	@ApiOperation(value = "专利OA答复模块获取节点列表", notes = "专利OA答复模块获取节点列表")
	@RequestMapping(value="/getNodeList",method = RequestMethod.GET)
	public Message getNodeList(@RequestParam String processType) {
		try {
			List<ProcessNodeVo> list = tbPamGeneralNodeMapper.getNodeList(processType);
			return Message.success("success", list);
		} catch (Exception e) {
			log.error("查询异常:{}", e);
			return Message.error(1, "失败");
		}

	}

}

